package algorithm.array;

import util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * leetcode : https://leetcode.com/problems/insert-interval/
 * Difficulty : Medium
 *
 * 给定一个已经排序好了的区间列表。并且保证不重合
 * 然后又给了一个新的区间，要插入到已有的区间列表，如果有重合则需要合并
 *
 *
 * Created by yzy on 2021-02-25 16:21
 */
public class InsertInterval {

    public static void main(String[] args) {
        int[][] inter = new int[][]{{1,2},{3,5},{6,7},{8,10},{12,16}};
        int[] newInter = new int[]{4,8};
        System.out.println(GsonUtil.toJson(insert(inter, newInter)));
    }

    public static int[][] insert(int[][] intervals, int[] newInterval) {
        if(intervals.length==0){
            return new int[][]{newInterval};
        }

        List<int[]> resList = new ArrayList<>();
        int startIdx = -1;
        int endId = -1;

        // TODO 和merge一样，就不做了

        int[][] resArr = new int[resList.size()][];
        int idx=0;
        for(int[] tmp : resList){
            resArr[idx++] = tmp;
        }
        return resArr;
    }
}
